﻿using System.Linq;
using Biz.Buffer;
using Biz.EF.Gateway.Monitor;
using Biz.EF.Platform.Kq;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    public class CommonProcessor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(CommonProcessor));
        public void Process()//string cn)
        {
            //if (string.IsNullOrEmpty(cn)) return;
            using (var gCtx = new GatewayMonitorContext())
            using (var pCtx = new PlatformYgContext())
            {
                var flag = ProcessFlag.NotProcessed.ToString();
                var items =
                    gCtx.received_msg.Where(t => t.is_length_right)
                        .Where(t => t.processed_flag.Equals(flag))
                       // .Where(t => t.cn.Equals(cn))
                        .OrderBy(t => t.datatime)
                        .Take(100)
                        .ToList();
                foreach (var item in items)
                {
                    Log.Info("Processing " + item.cn + ":" + item.received_string);
                    switch (item.cn)
                    {
                        case "2011":
                            ProcessorData.Process2011(pCtx, item.received_string);
                            var port = CommonInformation.GetLetportByMn(item.mn);
                            if (port != null)
                            {
                                ProcessorAlarm.ProcessB01EmissionRatio(pCtx, port);
                            }
                            break;
                        case "9011":
                            ProcessorResponse.Process9011(pCtx, item.received_string);
                            break;
                        case "9012":
                            ProcessorResponse.Process9012(pCtx, item.received_string);
                            break;
                        case "0000":
                            ProcessorStatus.Process0000(pCtx, item.received_string);
                            break;
                        case "9999":
                            ProcessorStatus.Process9999(pCtx, item.received_string);
                            break;
                        case "1011":
                            ProcessorStatus.Process1011(pCtx, item.received_string);
                            break;
                        case "3071":
                            ProcessorStatus.Process3071(pCtx, item.received_string);
                            break;
                        case "3081":
                            ProcessorStatus.Process3081(pCtx, item.received_string);
                            break;
                        case "3082":
                            ProcessorStatus.Process3082(pCtx, item.received_string);
                            break;
                        case "3083":
                            ProcessorStatus.Process3083(pCtx, item.received_string);
                            break;
                        case "3084":
                            ProcessorStatus.Process3084(pCtx, item.received_string);
                            break;
                        case "3085":
                            ProcessorStatus.Process3085(pCtx, item.received_string);
                            break;
                        case "3092":
                            ProcessorStatus.Process3092(pCtx, item.received_string);
                            break;
                        case "3098":
                            ProcessorStatus.Process3098(pCtx, item.received_string);
                            break;
                    }
                    item.processed_flag = ProcessFlag.ProcessingFinished.ToString();
                }
                gCtx.SaveChanges();
            }
        }
    }
}
